package cn.yunyichina.provider.pay.icbc.utils;

import java.io.File;  
import java.io.FileInputStream;  
import java.io.StringReader;  
import java.net.URL;  
import java.util.List;  
  
import org.dom4j.Document;  
import org.dom4j.Element;  
import org.dom4j.Node;  
import org.dom4j.io.SAXReader;  
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class XPathReader {
	 private String xml = null;  
     
	    private static Logger logger = LoggerFactory.getLogger(XPathReader.class); 
	      
	    public XPathReader(String xml){  
	        this.xml = xml;  
	    }             
	  
	    public XPathReader(Document xmlDocument){  
	        this.xmlDocument = xmlDocument;   
	    }  
	      
	    /** 
	     * 从一个xml加载数据 
	     * @param xml xml字符串 
	     */  
	    public static XPathReader createXmlPathReaderByXml(String xml){  
	        XPathReader xmlReader = null;  
	          
	        try {  
	            xmlReader = new XPathReader(xml);  
	        } catch (Exception e) {  
	            throw new RuntimeException(e);  
	        }         
	          
	        return xmlReader;  
	    }  
	      
	    /** 
	     * 从一个XML文件中读取数据 
	     * @param xmlFile xml文件 
	     * @return 
	     */  
	    public static XPathReader createXmlPathReaderByFile(File xmlFile){  
	        XPathReader xmlReader = null;  
	        Document xmlDocument = null;  
	          
	        try {  
	            xmlDocument = new SAXReader().read(new FileInputStream(xmlFile));  
	            xmlReader = new XPathReader(xmlDocument);  
	        } catch (Exception e) {  
	            throw new RuntimeException(e);  
	        }  
	          
	        return xmlReader;  
	    }  
	      
	    /** 
	     * 从系统环境中查找一个xml文件，加载数据 
	     * @param res xml文件资源 
	     * @return 
	     */  
	    public static XPathReader createXmlPathReaderByResource(String res){  
	        XPathReader xmlReader = null;  
	        Document xmlDocument = null;  
	        URL url = ClassLoader.getSystemResource(res);  
	          
	        if(null == url){  
	            throw new RuntimeException(res + " not found.");  
	        }  
	  
	        try {  
	            xmlDocument = new SAXReader().read(new FileInputStream(new File(url.toURI())));  
	            xmlReader = new XPathReader(xmlDocument);  
	        } catch (Exception e) {  
	            throw new RuntimeException(e);  
	        }  
	          
	        return xmlReader;  
	    }  
	      
	    /** 
	     * 返回xpath所表示的结点数 
	     * @param xpath 
	     * @return 
	     */  
	    public int countNode(String xpath){       
	        Document fileDocument = readDocument();  
	          
	        if(!findXPath(xpath)){  
	            return 0;  
	        }  
	          
	        List es = fileDocument.selectNodes(xpath);  
	          
	        if(null == es){  
	            return 0;  
	        }else{  
	            return es.size();  
	        }  
	    }  
	  
	    /** 
	     * 以xpath方式得到结果的文本 
	     * @param xpath 
	     * @return 如果不存在此结点,则返回"" 
	     */  
	    public String getText(String xpath){  
	        String text = "";  
	        Document fileDocument = readDocument();  
	          
	        if(!findXPath(xpath)){  
	            return "";  
	        }  
	          
	        Node node = fileDocument.selectSingleNode(xpath);  
	          
	        text = node.getText();  
	        return text;  
	    }     
	      
	    /** 
	     * 以xpath方式得到结点某属性的值 
	     * @param xpath 
	     * @param attrName 
	     * @return 
	     */  
	    public String getAttribute(String xpath, String attrName){  
	        String attr = "";  
	        Document fileDocument = readDocument();  
	          
	        if(!findXPath(xpath)){  
	            return "";  
	        }  
	          
	        List es = fileDocument.selectNodes(xpath);  
	        attr = ((Element)es.get(0)).attributeValue(attrName);  
	          
	        return attr;  
	    }  
	      
	    /** 
	     * 查找xpath是否存在 
	     * @param xpath 
	     * @return 存在返回true,否则返回false; 
	     */  
	    public boolean findXPath(String xpath){  
	        Document fileDocument = readDocument();  
	          
	        try{  
	            List es = fileDocument.selectNodes(xpath);  
	              
	            if(0 == es.size()){  
	                return false;  
	            }else{  
	                return true;  
	            }  
	        }catch(Exception ex){  
	            return false;  
	        }         
	    }  
	      
	    /** 
	     * 读取配置文件 
	     *  
	     * @return 返回Document对象 
	     */  
	    private Document readDocument() {  
	        SAXReader reader = new SAXReader();  
//	      Document fileDocument = null;  
	          
	        try {  
	            if(null == xmlDocument){  
	                xmlDocument = reader.read(new StringReader(xml));  
	            }                          
	        } catch(Exception ex){  
	            logger.error(ex.getMessage(), ex);  
	        }  
	          
	        return xmlDocument;  
	    }     
	      
	    private Document xmlDocument = null;  
}
